package com.easygem.system.service.impl;

import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.system.DeptClient;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.utils.DepUtils;
import com.easygem.common.utils.DeptTreeBuilder;
import com.easygem.common.vo.system.DepartmentVO;
import com.easygem.common.vo.system.DeptNodeVO;
import com.easygem.system.converter.DepartmentConverter;
import com.easygem.system.mapper.DeptClientMapper;
import com.easygem.system.mapper.UserMapper;
import com.easygem.system.service.DeptService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 文晓平
 * @version 1.0
 * @Description 部门业务层的实现
 * @date 2024/12/25 20:53
 */
@Service
public class DeparmentClientServiceImpl implements DeptService {
    @Autowired
    private DeptClientMapper deptClientMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 获取部门树
     *
     * @return
     */
    @Override
    public List<DeptNodeVO> findDeptTree() {
        List<DeptClient> deptClients = deptClientMapper.selectAll();
        List<DeptNodeVO> departmentVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(deptClients)) {
            for (DeptClient department : deptClients) {
                DeptNodeVO d = new DeptNodeVO();
                BeanUtils.copyProperties(department, d);
                Example o = new Example(User.class);
                o.createCriteria().andEqualTo("deptId",department.getDeptId())
                        .andNotEqualTo("type",0).andNotEqualTo("status",-2);
                d.setTotal(userMapper.selectCountByExample(o));
                departmentVOS.add(d);
            }
        }
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
//        Dept deptDb = new Dept();
//        deptDb.setDeptId(activeUser.getEmployee().getDeptId());
//        Dept dept = deptMapper.selectOne(deptDb);
//        DeptNodeVO d = new DeptNodeVO();
//        d.setDeptId(dept.getDeptId());
//        d.setDeptName(dept.getDeptName());
//        Example o = new Example(User.class);
//        o.createCriteria().andEqualTo("deptId",dept.getDeptId())
//                .andNotEqualTo("type",0).andNotEqualTo("status",-2);
//        d.setTotal(employeeMapper.selectCountByExample(o));
        return DeptTreeBuilder.build(departmentVOS);
        //return DepUtils.getDepartmentVOAndChildren(departmentVOS,d);
    }

    /**
     * 根据当前部门编号获得当前部门及以下所有部门信息
     *
     * @return
     */
    @Override
    public List<DeptClient> findDeptTreeByDeptId() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        DeptClient deptClientDb = new DeptClient();
        deptClientDb.setDeptId(activeUser.getEmployee().getDeptId());
        DeptClient deptClient = deptClientMapper.selectOne(deptClientDb);
        List<DeptClient> deptClients = deptClientMapper.selectAll();
        return DepUtils.getDepartmentAndChildrenIds(deptClients, deptClient);
    }

    /**
     * 添加部门
     *
     * @param departmentVO
     */
    @Override
    public DeptClient add(DepartmentVO departmentVO) {
        DeptClient deptClient = new DeptClient();
        BeanUtils.copyProperties(departmentVO, deptClient);
        //获得到登录后的用户信息，通用方式
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        deptClientMapper.insert(deptClient);
        return deptClient;
    }

    /**
     * 删除节点
     *
     * @param id
     */
    @Override
    public void delete(Long id) throws SystemException {
        DeptClient deptClient = deptClientMapper.selectByPrimaryKey(id);
        if(deptClient ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的部门不存在");
        }
        deptClientMapper.deleteByPrimaryKey(id);
    }

    /**
     * 删除节点
     *
     * @param id
     */
    @Override
    public void allDelete(Long id) throws SystemException {
        DeptClient deptClient = deptClientMapper.selectByPrimaryKey(id);
        if(deptClient ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的部门不存在");
        }
        List<Long> ids = DepUtils.getDepartmentIdAndChildrenIds(deptClientMapper.selectAll(), id);
        deptClientMapper.allDelete(ids);
        deptClientMapper.deleteInfoByUserStatus();
    }

    /**
     * 删除节点
     *
     * @param id
     */
    @Override
    public void allUpdate(Long id) throws SystemException {
        DeptClient deptClient = deptClientMapper.selectByPrimaryKey(id);
        if(deptClient ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的部门不存在");
        }
        List<Long> ids = DepUtils.getDepartmentIdAndChildrenIds(deptClientMapper.selectAll(), id);
        deptClientMapper.allUpdate(ids);
        deptClientMapper.updateInfoByUserStatus();
    }

    /**
     * 编辑节点
     *
     * @param id
     * @return
     */
    @Override
    public DepartmentVO edit(Long id) throws SystemException {
        DeptClient deptClient = deptClientMapper.selectByPrimaryKey(id);
        if(deptClient ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该编辑的部门不存在");
        }
        return DepartmentConverter.converterToDepartmentVO(deptClient);
    }

    /**
     * 更新部门
     *
     * @param id
     * @param departmentVO
     */
    @Override
    public void update(Long id, DepartmentVO departmentVO) throws SystemException {
        DeptClient dbDeptClient = deptClientMapper.selectByPrimaryKey(id);
        if(dbDeptClient ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要更新的部门不存在");
        }
        DeptClient deptClient = new DeptClient();
        BeanUtils.copyProperties(departmentVO, deptClient);
        deptClient.setDeptId(id);
        deptClientMapper.updateByPrimaryKeySelective(deptClient);
    }

    /**
     * 所有展开部门的ID
     *
     * @return
     */
    @Override
    public List<Long> findOpenIds() {
        List<Long> ids=new ArrayList<>();
        List<DeptClient> deptClients = deptClientMapper.selectAll();
        if(!CollectionUtils.isEmpty(deptClients)){
            for (DeptClient deptClient : deptClients) {
//                if(menu.getOpen()==1){
                    ids.add(deptClient.getDeptId());
//                }
            }
        }
        return ids;
    }

    /**
     * 获取所有菜单
     *
     * @param accountName
     * @return
     */
    @Override
    public List<DeptNodeVO> findDeptByAccountName(String accountName) {
        List<DeptNodeVO> nodeVOS=new ArrayList<>();
        return nodeVOS;
    }

    /**
     * 获取所有菜单
     *
     * @return
     */
    @Override
    public List<DeptClient> findAll() {
        return deptClientMapper.selectAll();
    }

    /**
     * 获取没有分配客户的部门
     *
     * @return
     */
    @Override
    public List<DeptClient> findAccountDept() {
        return deptClientMapper.selectAccountDept();
    }
}
